package com.bluedot.www.core.service;

import com.bluedot.www.core.dao.Execute;
import com.bluedot.www.core.pojo.DO.Right;
import com.bluedot.www.core.pojo.DO.Role;
import com.bluedot.www.core.pojo.DTO.RoleDTO;
import com.bluedot.www.core.utils.DbUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ZhouXuan
 * @version 1.0
 * @date 2021/9/4 16:06
 */
public class RoleService {
    private static final String ROLE_MAPPER_ADDRESS = "com.bluedot.www.core.mapper.xml.RoleMapper.";

    private static final String RIGHT_MAPPER_ADDRESS = "com.bluedot.www.core.mapper.xml.RightMapper.";


    public static Object execute(String methodName, Object[] param) {
        RoleService roleService = new RoleService();
        Object result = null;
        switch (methodName) {
            case "findById":
                result = roleService.findById((Long) param[0]);
                break;
            case "insertRole":
                result = roleService.insertRole((Role) param[0]);
                break;
            case "insertRightByRoleId":
                result = roleService.insertRightByRoleId((List<Long>) param[0], (Long) param[1]);
                break;
            case "deleteRightByRoleId":
                result = roleService.deleteRightByRoleId((List<Long>) param[0], (Long) param[1]);
                break;
            case "deleteRoleByRoleId":
                result = roleService.deleteRoleByRoleId((List<Long>) param[0]);
                break;
            default:
                throw new RuntimeException("Method NOT FOUND, method name: " + methodName);
        }
        return result;
    }


    /**
     * 查询一个角色详情，包括角色已有权限和未有权限
     *
     * @param roleId:
     * @return com.bluedot.www.core.pojo.DTO.RoleDTO
     * @author ZhouXuan
     * @date 2021/9/5 16:49
     */
    private RoleDTO findById(Long roleId) {
        RoleDTO roleDTO = new RoleDTO();
        // 得到角色信息
        List<Role> roles = (List<Role>) Execute.execute(Execute.QUERY_TYPE, ROLE_MAPPER_ADDRESS + "findRoleByRoleId", new Object[]{roleId});
        roleDTO.setRole(roles.get(0));

        // 得到角色已有权限列表
        List<Right> existRights = (List<Right>) Execute.execute(Execute.QUERY_TYPE, RIGHT_MAPPER_ADDRESS + "findRightsByRoleId", new Object[]{roleId});
        roleDTO.setExistRights(existRights);

        // 得到角色未有权限列表
        List<Right> notExistRights = (List<Right>) Execute.execute(Execute.QUERY_TYPE, RIGHT_MAPPER_ADDRESS + "findOtherRightsByRoleId", new Object[]{roleId});
        roleDTO.setNotExistRights(notExistRights);

        return roleDTO;
    }

    /**
     * 根据角色实体类，添加、插入一位角色
     *
     * @param role 角色类DO
     * @return boolean
     * @author DengHongWei
     * @date 2021/8/8 15:41
     */
    private boolean insertRole(Role role) {
        // 插入前判断roleName是否存在
        List<Role> roles = (List<Role>) Execute.execute(Execute.QUERY_TYPE, ROLE_MAPPER_ADDRESS + "findRoleByName", new Object[]{role.getRoleName()});

        // 如果roleName不存在，则插入数据
        if (roles.size() == 0) {
            // 设置主键
            role.setId(DbUtil.generateId());

            // 设置时间
            role.setGmtCreate(DbUtil.getCurrentTime());
            role.setGmtModified(DbUtil.getCurrentTime());

            Integer count = (Integer) Execute.execute(Execute.UPDATE_TYPE, ROLE_MAPPER_ADDRESS + "insertRole", new Object[]{role});
            return count > 0;
        }
        return false;
    }


    /**
     * 根据角色id，添加、插入多条权限
     *
     * @param rightIds:
     * @param roleId:
     * @return boolean
     * @author ZhouXuan
     * @date 2021/9/4 16:20
     */
    private boolean insertRightByRoleId(List<Long> rightIds, Long roleId) {
        Map<String, String> map1 = null;
        Map<String, String> map2 = new HashMap<>();

        map2.put("roleId", roleId.toString());

        Integer sum = 0;
        Integer count;
        for (Long rightId : rightIds) {
            map1 = new HashMap<>();
            map1.put("rightId", rightId.toString());
            count = (Integer) Execute.execute(Execute.UPDATE_TYPE, ROLE_MAPPER_ADDRESS + "insertRightByRoleId", new Object[]{map1, map2});
            sum += count;
        }

        return rightIds.size() == sum;
    }

    /**
     * 根据角色id，删除多条权限，删除是只是此角色的该权限被删除。删的是权限-角色中间表
     *
     * @param rightIds:
     * @param roleId:
     * @return boolean
     * @author ZhouXuan
     * @date 2021/9/4 16:28
     */
    private boolean deleteRightByRoleId(List<Long> rightIds, Long roleId) {
        Map<String, String> map1 = null;
        Map<String, String> map2 = new HashMap<>();

        map2.put("roleId", roleId.toString());

        Integer sum = 0;
        Integer count;
        for (Long rightId : rightIds) {
            map1 = new HashMap<>();
            map1.put("rightId", rightId.toString());
            count = (Integer) Execute.execute(Execute.UPDATE_TYPE, ROLE_MAPPER_ADDRESS + "deleteRightByRoleId", new Object[]{map1, map2});
            sum += count;
        }

        return rightIds.size() == sum;
    }

    /**
     * 根据角色id，删除多位角色
     *
     * @param roleIds:
     * @return boolean
     * @author ZhouXuan
     * @date 2021/9/4 16:32
     */
    private boolean deleteRoleByRoleId(List<Long> roleIds) {

        Integer sum = 0;
        Integer count;

        for (Long roleId : roleIds) {
            // 1.删除角色与权限中间表的内容
            Integer count1 = (Integer) Execute.execute(Execute.UPDATE_TYPE, ROLE_MAPPER_ADDRESS + "deleteRoleRightByRoleId", new Object[]{roleId});

            // 2.删除角色与用户中间表的内容
            Integer count2 = (Integer) Execute.execute(Execute.UPDATE_TYPE, ROLE_MAPPER_ADDRESS + "deleteUserRoleByRoleId", new Object[]{roleId});

            // 3.删除角色表中的内容
            count = (Integer) Execute.execute(Execute.UPDATE_TYPE, ROLE_MAPPER_ADDRESS + "deleteRole", new Object[]{roleId});
            sum += count;
        }

        return roleIds.size() == sum;
    }

}
